package com.xwy.service.impl;

import com.github.pagehelper.PageHelper;
import com.xwy.dao.SysApplyDao;
import com.xwy.dao.SysDepartmentDao;
import com.xwy.dao.SysUserDao;
import com.xwy.entity.SysApply;
import com.xwy.entity.SysDepartment;
import com.xwy.entity.SysUser;
import com.xwy.service.SysApplyService;
import com.xwy.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class SysApplyServiceImpl implements SysApplyService {

    @Resource
    private SysApplyDao sysApplyDao;
    @Resource
    private SysDepartmentDao sysDepartmentDao;
    @Resource
    private SysUserDao sysUserDao;

    @Override
    public boolean usersApply(String aid, String uno) {
        boolean usersApply = sysApplyDao.usersApply(aid, uno);
        return usersApply;
    }

    @Override
    public SysApply findByAid(String aid, String uno) {
        SysApply byAid = sysApplyDao.findByAid(aid, uno);
        return byAid;
    }

    @Override
    public List<SysApply> findByUno(String uno, int pageNum, int pageSize) {
        //pageNum代表页码值，pageSize代表每页条数
        PageHelper.startPage(pageNum, pageSize);
        List<SysApply> applyDaoByUno = sysApplyDao.findByUno(uno);
        return applyDaoByUno;
    }

    @Override
    public boolean deleteByAid(String aid) {
        boolean deleteByAid = sysApplyDao.deleteByAid(aid);
        return true;
    }

    @Override
    public SysApply deleteApply(SysApply sysApply) {
        SysApply deleteApply = sysApplyDao.deleteApply(sysApply.getAid(), sysApply.getUno());
        return deleteApply;
    }

    @Override
    public int status(String aid) {
        int status = sysApplyDao.status(aid);
        return status;
    }

    @Override
    public SysApply userApply(String depId, String uno, String cause) throws ParseException {
        SysApply sysApply = new SysApply();
        SysDepartment sysDepartment = sysDepartmentDao.queryById(depId);
        SysUser sysUser = sysUserDao.queryByUno(uno);
        sysApply.setAid(UUIDUtil.getUUID());
        sysApply.setDepartment(sysDepartment);
        sysApply.setUno(sysUser.getUno());
        sysApply.setCause(cause);
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(day));
        sysApply.setCreateTime(df.parse(df.format(day)));
        //插入数据库
        boolean result = sysApplyDao.insert(sysApply);
        if (result) {
            return sysApply;
        } else {
            return null;
        }
    }

    @Override
    public int update(SysApply sysApply) {
        int i = sysApplyDao.update(sysApply);
        return i;
    }

    @Override
    public boolean updateStatus(Integer apply_status, String aid) {
        boolean updateStatus = sysApplyDao.updateStatus(apply_status, aid);
        return updateStatus;
    }

}
